#ifndef CARDATAMODEL_H
#define CARDATAMODEL_H

#include <QObject>
#include <QString>
#include <QTimer>
#include <QTime>

class CarDataModel : public QObject
{
    Q_OBJECT

    Q_PROPERTY(int rpm READ rpm WRITE setRpm NOTIFY rpmChanged)
    Q_PROPERTY(int speed READ speed WRITE setSpeed NOTIFY speedChanged)
    Q_PROPERTY(double temperature READ temperature WRITE setTemperature NOTIFY temperatureChanged)
    Q_PROPERTY(QString currentTime READ currentTime NOTIFY timeChanged)
    Q_PROPERTY(double outsideTemp READ outsideTemp WRITE setOutsideTemp NOTIFY outsideTempChanged)
    Q_PROPERTY(int totalKm READ totalKm WRITE setTotalKm NOTIFY totalKmChanged)
    Q_PROPERTY(int remainingKm READ remainingKm WRITE setRemainingKm NOTIFY remainingKmChanged)
    Q_PROPERTY(int waterTemp READ waterTemp WRITE setWaterTemp NOTIFY waterTempChanged)
    Q_PROPERTY(int fuelLevel READ fuelLevel WRITE setFuelLevel NOTIFY fuelLevelChanged)
    Q_PROPERTY(QString statusMessage READ statusMessage WRITE setStatusMessage NOTIFY statusMessageChanged)
    Q_PROPERTY(bool engineRunning READ engineRunning WRITE setEngineRunning NOTIFY engineRunningChanged)
    Q_PROPERTY(bool handbrakeOn READ handbrakeOn WRITE setHandbrakeOn NOTIFY handbrakeOnChanged)
    Q_PROPERTY(bool seatbeltWarning READ seatbeltWarning WRITE setSeatbeltWarning NOTIFY seatbeltWarningChanged)
    Q_PROPERTY(bool headlightsOn READ headlightsOn WRITE setHeadlightsOn NOTIFY headlightsOnChanged)
    Q_PROPERTY(bool highBeamOn READ highBeamOn WRITE setHighBeamOn NOTIFY highBeamOnChanged)
    Q_PROPERTY(int oilLife READ oilLife WRITE setOilLife NOTIFY oilLifeChanged)
    Q_PROPERTY(double avgSpeed READ avgSpeed WRITE setAvgSpeed NOTIFY avgSpeedChanged)
    Q_PROPERTY(double avgFuelConsumption READ avgFuelConsumption WRITE setAvgFuelConsumption NOTIFY avgFuelConsumptionChanged)
    Q_PROPERTY(int tripDistance READ tripDistance WRITE setTripDistance NOTIFY tripDistanceChanged)

public:
    explicit CarDataModel(QObject *parent = nullptr);

    // Getters
    int rpm() const { return m_rpm; }
    int speed() const { return m_speed; }
    double temperature() const { return m_temperature; }
    QString currentTime() const { return QTime::currentTime().toString("hh:mm"); }
    double outsideTemp() const { return m_outsideTemp; }
    int totalKm() const { return m_totalKm; }
    int remainingKm() const { return m_remainingKm; }
    int waterTemp() const { return m_waterTemp; }
    int fuelLevel() const { return m_fuelLevel; }
    QString statusMessage() const { return m_statusMessage; }
    bool engineRunning() const { return m_engineRunning; }
    bool handbrakeOn() const { return m_handbrakeOn; }
    bool seatbeltWarning() const { return m_seatbeltWarning; }
    bool headlightsOn() const { return m_headlightsOn; }
    bool highBeamOn() const { return m_highBeamOn; }
    int oilLife() const { return m_oilLife; }
    double avgSpeed() const { return m_avgSpeed; }
    double avgFuelConsumption() const { return m_avgFuelConsumption; }
    int tripDistance() const { return m_tripDistance; }

    // Setters
    void setRpm(int rpm);
    void setSpeed(int speed);
    void setTemperature(double temperature);
    void setOutsideTemp(double outsideTemp);
    void setTotalKm(int totalKm);
    void setRemainingKm(int remainingKm);
    void setWaterTemp(int waterTemp);
    void setFuelLevel(int fuelLevel);
    void setStatusMessage(const QString &statusMessage);
    void setEngineRunning(bool engineRunning);
    void setHandbrakeOn(bool handbrakeOn);
    void setSeatbeltWarning(bool seatbeltWarning);
    void setHeadlightsOn(bool headlightsOn);
    void setHighBeamOn(bool highBeamOn);
    void setOilLife(int oilLife);
    void setAvgSpeed(double avgSpeed);
    void setAvgFuelConsumption(double avgFuelConsumption);
    void setTripDistance(int tripDistance);

public slots:
    void toggleEngine();
    void toggleHandbrake();
    void toggleHeadlights();
    void toggleHighBeam();
    void resetTrip();

signals:
    void rpmChanged();
    void speedChanged();
    void temperatureChanged();
    void timeChanged();
    void outsideTempChanged();
    void totalKmChanged();
    void remainingKmChanged();
    void waterTempChanged();
    void fuelLevelChanged();
    void statusMessageChanged();
    void engineRunningChanged();
    void handbrakeOnChanged();
    void seatbeltWarningChanged();
    void headlightsOnChanged();
    void highBeamOnChanged();
    void oilLifeChanged();
    void avgSpeedChanged();
    void avgFuelConsumptionChanged();
    void tripDistanceChanged();

private slots:
    void updateTime();
    void simulateData();

private:
    int m_rpm;
    int m_speed;
    double m_temperature;
    double m_outsideTemp;
    int m_totalKm;
    int m_remainingKm;
    int m_waterTemp;
    int m_fuelLevel;
    QString m_statusMessage;
    bool m_engineRunning;
    bool m_handbrakeOn;
    bool m_seatbeltWarning;
    bool m_headlightsOn;
    bool m_highBeamOn;
    int m_oilLife;
    double m_avgSpeed;
    double m_avgFuelConsumption;
    int m_tripDistance;

    // 目标值，用于平滑动画过渡
    int m_targetRpm;
    int m_targetSpeed;
    int m_targetWaterTemp;
    int m_targetFuelLevel;

    QTimer *m_timeTimer;
    QTimer *m_simulationTimer;
};

#endif // CARDATAMODEL_H
